package configlogic

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/recharge"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetRechargeTypeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetRechargeTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRechargeTypeLogic {
	return &GetRechargeTypeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetRechargeTypeLogic) GetRechargeType(in *rpc.RechargeTypeRequest) (*rpc.RechargeTypeResponse, error) {
	var rechargeTypes []*recharge.RechargeType
	var userBankInfoList []*rpc.RechargeTypeInfo

	// 查询所有用户银行信息
	query := l.svcCtx.Db
	if in.Id != 0 {
		query = query.Where("id = ?", in.Id)
	}

	if in.Name != "" {
		query = query.Where("name = ?", in.Name)
	}

	tx := query.Find(&rechargeTypes)
	if tx.Error != nil {
		return nil, tx.Error
	}

	for _, rechargeType := range rechargeTypes {
		userBankInfo := &rpc.RechargeTypeInfo{
			Id: rechargeType.ID,
		}
		userBankInfoList = append(userBankInfoList, userBankInfo)
	}

	return &rpc.RechargeTypeResponse{
		RechargeTypeInfoList: userBankInfoList,
	}, nil
}
